layui.define(['table', 'form'], function (exports) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , form = layui.form;

    //幻灯片管理
    table.render({
        elem: '#LAY-set-slide-list'
        , url: layui.setter.host + '/slide'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'name', title: '名称', minWidth: 100}
            , {field: 'sort', width: 80, title: '排序'}
            , {field: 'status', title: '状态', templet: '#statusTpl', width: 80, align: 'center'}
            , {field: 'create_time', title: '创建时间', sort: true}
            , {title: '操作', width: 250, align: 'center', fixed: 'right', toolbar: '#layuiadmin-set-menusbar'}
        ]]
        , page: true
        , limit: 30
        , height: 'full-320'
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-set-slide-list)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.prompt({
                formType: 1
                , title: '敏感操作，请验证口令'
            }, function (value, index) {
                layer.close(index);

                layer.confirm('真的删除行么', function (index) {
                    admin.req({
                        url: layui.setter.host + '/slide/' + data.id
                        , type: "delete"
                        , done: function (res) {
                            obj.del();
                            layer.close(index);
                        }
                    });
                });
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑幻灯片'
                , area: ['400px', '300px']
                , id: 'LAY-set-slide-edit'
                , success: function (layero, index) {
                    view(this.id).render('set/slide/listform', data).done(function () {
                        form.render(null, 'LAY-set-slide-form');

                        //监听提交
                        form.on('submit(LAY-set-slide-submit)', function (form) {
                            admin.req({
                                url: layui.setter.host + '/slide/' + data.id
                                , data: form.field
                                , type: "put"
                                , done: function (res) {
                                    obj.update(res.data); //数据更新
                                    layer.close(index); //执行关闭
                                }
                            });
                        });
                    });
                }
            });
        }
    });

    //幻灯片子项目管理
    table.render({
        elem: '#LAY-set-item-list'
        , url: layui.setter.host + '/slide/' + layui.router().search.slide_id + '/item'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'title', title: '名称', minWidth: 100}
            , {field: 'image', title: '缩略图', width: 100, templet: '#imgTpl'}
            , {field: 'url', title: '地址'}
            , {field: 'sort', width: 80, title: '排序'}
            , {field: 'status', title: '状态', templet: '#statusTpl', width: 80, align: 'center'}
            , {field: 'create_time', title: '创建时间', sort: true}
            , {title: '操作', minWidth: 180, align: 'center', fixed: 'right', toolbar: '#LAY-set-item-bar'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });

    //监听工具条
    table.on('tool(LAY-app-course-lesson)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此章节？', function (index) {
                admin.req({
                    url: layui.setter.host + '/slide/' + data.slide_id + '/item/' + data.id,
                    type: "delete",
                    success: function (res) {
                        if (res.code === 0) {
                            obj.del();
                            layer.msg(res.message, {icon: 6});
                        } else {
                            layer.msg(res.message, {icon: 5});
                        }
                        layer.close(index);
                    }
                });
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑章节'
                , area: ['800px', '600px']
                , id: 'LAY-app-course-lesson-edit'
                , success: function (layero, index) {
                    view(this.id).render('app/course/lessonform', data).done(function () {
                        form.render(null, 'LAY-app-course-lesson-form');

                        //监听提交
                        form.on('submit(LAY-app-course-lesson-submit)', function (res) {
                            admin.req({
                                url: layui.setter.host + '/slide/' + layui.router().search.slide_id + '/item/' + data.id
                                , data: res.field
                                , type: "put"
                                , done: function (res) {
                                    if (!res.code) {
                                        layer.msg(res.message, {icon: 6});
                                        obj.update(form.field);
                                    } else {
                                        layer.msg(res.message, {icon: 5});
                                    }
                                    layer.close(index);
                                }
                            });
                        });
                    });
                }
            });
        }
    });

    exports('slide', {})
});